export default {
	name: "user-center",
	data() {
		return {
			activeName: "user-info",
			tabPosition: "top",
			formItems: [
				{label: "工号", kind: "input", prop: "id", isEdit: false, disabled: true},
				{label: "用户名", kind: "input", prop: "userName", isEdit: false, disabled: true},
				{label: "职位", kind: "input", prop: "jobName", isEdit: false, disabled: true},
				{label: "权限", kind: "input", prop: "permission", isEdit: false, disabled: true},
				{label: "手机号", kind: "input", prop: "phone", isEdit: false, disabled: true},
				{label: "邮箱", kind: "input", prop: "mail", isEdit: false, disabled: true}
			],
			formParams: {
				"id": 1000,
				"userName": "",
				"jobName": "员工",
				"permission": 1,
				"phone": "12345678901",
				"mail": "57761505@qq.com"
			},
			updateUser: {
				userName: "",
				telephone: "",
				verifyCode: "",
				newPassword: "",
				rePassword: ""
			},
			pwdRules: {
				userName: [
					{required: true, message: "请输入账号", trigger: "blur"/*, validator: validateUsername*/},
					{min: 1, max: 16, message: "长度在 1 到 16 个字符", trigger: "blur"}
				],
				telephone: [
					{required: true, message: "请输入手机号", trigger: "blur"/*, validator: validateUsername*/},
					{min: 11, max: 11, message: "手机号长度为11", trigger: "blur"}
				],
				verifyCode: [
					{required: true, message: "请输入密码", trigger: "blur"/*, validator: validatePass*/},
					{min: 6, max: 6, message: "长度为 6 个字符", trigger: "blur"}
				],
				newPassword: [
					{required: true, message: "请输入新密码", trigger: "blur"/*, validator: validatePass*/},
					{min: 5, max: 16, message: "长度在 5 到 16 个字符", trigger: "blur"}
				],
				rePassword: [
					{required: true, message: "请再次输入新密码", trigger: "blur"/*, validator: validatePass*/},
					{min: 5, max: 16, message: "长度在 5 到 16 个字符", trigger: "blur"}
				]
			},
			src: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
			upLoadUrl: "xxxxxxxxxx",
			imageUrl: ""
		};
	},
	created() {
		this.initUserInfo();
	},
	methods: {
		initUserInfo() {
			this.formParams.userName = this.user.userName;
			this.formParams.permission = this.user.permission;
		},
		handleClick(tab, event) {
			if (tab.name === "update-pwd") {
				this.updateUser = {};
				this.updateUser.userName = this.user.userName;
				this.updateUser.telephone = this.formParams.phone;
				this.updateUser.verifyCode = "";
			}
			this.formItems.forEach(form => {
				if (form.name !== "id" && form.name !== "permission" && form.name !== "jobName") {
					if (tab.name === "user-info") {
						this.activeName = "user-info";
						form.isEdit = false;
						form.disabled = true;
					} else if (tab.name === "update-info") {
						this.activeName = "update-info";
						form.isEdit = true;
						form.disabled = false;
					}
				}
			});
		},
		//修改密码
		updatePwd() {
			this.$refs.updateForm.validate((valid) => {
				if (valid) {
					this.fetch.updatePwd(this.updateUser,
						(res) => {
							this.message.success("密码修改成功!");
							//修改密码成功后跳转到登录界面
							this.$router.push("/");
						}, (error) => {
							console.log(error);
						});
				} else {
					this.message.error("表单数据提交错误!");
					return false;
				}
			});
		},
		getVerifyCode() {
			console.log(11111111111111);
		},
		handleSave(formParams) {
			this.formParams = formParams;
			console.log(this.formParams);
			console.log(this.beforeAvatarUpload({
				name: "food.jpeg",
				url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100"
			}));
		},
		handleAvatarSuccess(res, file) {
			this.imageUrl = URL.createObjectURL(file.raw);
		},
		beforeAvatarUpload(file) {
			const isJPG = this.dataType.matchSuffix(file.name) === "image";
			const isLt2M = file.size / 1024 / 1024 < 2;
			if (!isJPG) {
				this.message.error("上传头像图片只能是 JPG 格式!");
			}
			if (!isLt2M) {
				this.message.error("上传头像图片大小不能超过 2MB!");
			}
			return isJPG && isLt2M;
		}
	},
	watch: {
		$route(to) {
			if (to && to.params && to.params.activeName) {
				this.activeName = to.params.activeName;
				this.$emit("reloadView");//刷新页面数据
			}
		}
	}
};
